package com.zi.experttalk.experttalk.Model.service;

import android.os.AsyncTask;
import android.util.Log;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;


/**
 * Created by Administrator on 2014/12/15.
 */
public class HttpManager {
   /*
   向服务器发送请求并取回数据
    */
    private  String getedData;
    private static final String locationUrl="http://192.168.199.182/youhui/mapi/index.php?i_type=0&r_type=0&soft_type=futuan&dev_type=android&requestData=";
    public void getHttpInfo(String param , final GetDataResultListener getDataResultListener) throws IOException {
        //请求数据
        final Base64Manager base64Manager = new Base64Manager();
        String encodeParam = base64Manager.base64encode(param);
        Log.d("encode", encodeParam);
        final String httpUrl = locationUrl + encodeParam;
        Log.d("url", httpUrl);

        //  Okhttp方法
        OkHttpClient okHttpClient = new OkHttpClient();
        final Request request = new Request.Builder()
                .url(httpUrl)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                if (getDataResultListener != null) {
                    getDataResultListener.onResult(null);
                    Log.d("aa", "11111111111111111");
                }
            }

            @Override
            public void onResponse(Response response) throws IOException {
                if (getDataResultListener != null) {
                    getedData = base64Manager.base64decode(response.body().string());
                    getDataResultListener.onResult(getedData);
                    Log.d("bb", "22222222222222222222222");
                    Log.d("get", getedData);
                }

            }
        });
        //异步方法
//        AsyncTask<Integer,String,String> asyncTask=new AsyncTask<Integer, String, String>() {
//           private String getData;
//            @Override
//            protected String doInBackground(Integer... params) {
//                HttpGet httpGet=new HttpGet(httpUrl);
//                HttpClient httpClient=new DefaultHttpClient();
//                try {
//                 HttpResponse response= httpClient.execute(httpGet);
//                 HttpEntity httpEntity=response.getEntity();
//                 InputStream inputStream=httpEntity.getContent();
//                 ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
//                    byte[] bytes=new byte[1024];
//                    int len=0;
//                    while ((len=inputStream.read(bytes))!=-1){
//                          byteArrayOutputStream.write(bytes,0,len);
//                    }
//                     getData=byteArrayOutputStream.toString();
//                } catch (IOException e) {
//                    Log.d("er","网络错误");
//                }
//                Log.d("getData",getData);
//                return getData;
//
//            }
//
//            @Override
//            protected void onPostExecute(String getData) {
//                if(getData!=null){
//                    if(getDataResultListener!=null){
//                        getDataResultListener.onResult(base64Manager.base64decode(getData));
//                    }
//                }
//            }
//        };
//        asyncTask.execute();
    }

    public  interface  GetDataResultListener{
        public void onResult(String data);
    }
}
